//
// Created by hps on 24-4-6.
//

#include "EditorBase.h"

namespace hps {
EditorBase::EditorBase(std::string filePath)
    : m_filePath(filePath) {
    m_layout = new QVBoxLayout();
    m_toolFrame = new QFrame();
    m_toolLayout = new QHBoxLayout();
    m_editorFrame = new QFrame();
    m_editorLayout = new QHBoxLayout();
    m_textEdit = new hps::TextEdit();
    m_isFileSave = true;
    m_dateTime = QDateTime::currentDateTime();
    m_out = nullptr;
    init();
    setUi();
    openFile();
    rspFunc();
}
EditorBase::EditorBase() {
    m_layout = new QVBoxLayout();
    m_toolFrame = new QFrame();
    m_toolLayout = new QHBoxLayout();
    m_editorFrame = new QFrame();
    m_editorLayout = new QHBoxLayout();
    m_textEdit = new hps::TextEdit();
    m_isFileSave = true;
    m_out = nullptr;
    init();
    setUi();
    openFile();
    rspFunc();
}
void EditorBase::init() {
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->setSpacing(0);
    m_editorLayout->setContentsMargins(0, 0, 0, 0);
    m_editorLayout->setSpacing(0);
}
void EditorBase::setUi() {
    this->setLayout(m_layout);
    m_layout->insertWidget(0, m_toolFrame);
    m_toolFrame->setLayout(m_toolLayout);
    m_layout->insertWidget(1, m_editorFrame);
    m_editorFrame->setLayout(m_editorLayout);
    m_editorLayout->insertWidget(0, m_textEdit);
    hideToolBar();
}

void EditorBase::hideToolBar() {
    m_toolFrame->hide();
}

void EditorBase::showToolBar() {
    m_toolFrame->show();
}
std::string EditorBase::getFileName() {
    int i = m_filePath.size() - 1;
    while (m_filePath[i] != '/')
        i --;
    return m_filePath.substr(i + 1);
}

void EditorBase::openFile() {
    // 打开文件
    m_file = new QFile(QString(m_filePath.c_str()));
    if (! m_file->open(QIODevice::ReadWrite | QIODevice::Text)) {
        std::cerr << "Failed to open file:" << m_file->errorString().toStdString() << std::endl;
        return;
    }

    // 读取文件内容
    QTextStream *in = new QTextStream(m_file);
    QString *fileContent = new QString(in->readAll());

    // 将文件内容显示到文本编辑器中
    m_textEdit->setPlainText(*fileContent);

    delete in;
    delete fileContent;
}

EditorBase::~EditorBase() {
    m_file->close();
    if (m_out)
        delete m_out;
}
void EditorBase::saveFile() {
    if (m_filePath.empty()) return;

    QFileInfo info(*m_file);
    if (m_dateTime >= info.lastModified()) { // 被修改
        reopen(m_file, QIODevice::WriteOnly | QIODevice::Truncate);
        m_out = new QTextStream(m_file);
        *m_out << m_textEdit->toPlainText();
    }
    m_isFileSave = true;
    reopen(m_file, QIODevice::ReadOnly | QIODevice::Text);
}
void EditorBase::textChange() {
    // bug: 派生的时候对文字进行高亮改变了文本，导致每次打开都需要保存，可以考虑将EditBase进行分割，派生出一个其他文本类，
    // 派生类打开文件这样就不会出现改变文本的情况
    m_isFileSave = false;
    m_dateTime = QDateTime::currentDateTime();
}
void EditorBase::rspFunc() {
    QObject::connect(m_textEdit, &TextEdit::textChanged, this, &EditorBase::textChange);
}
void EditorBase::reopen(QFile* file, QIODevice::OpenMode device) {
    file->close();
    file->open(device);
}

} // hps